package com.woniu.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniu.entity.vo.UserCountDayVO;
import com.woniu.entity.vo.UserLoginVO;
import com.woniu.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author xiaosong
 * @since 2023-04-01
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {


    @Update("update wnsys_user set user_type=#{user_type} where id=#{id}")
    int updateType(@Param("id") Integer id, @Param("user_type") Integer userType);

    @Select("select id, mobile, name, avatar from wnsys_user where mobile = #{mobile}")
    @ResultType(UserLoginVO.class)
    UserLoginVO login(String mobile);

    @Select("select id, mobile, name, avatar from wnsys_user where openid = #{giteeId}")
    @ResultType(UserLoginVO.class)
    UserLoginVO getByOpenId(String giteeId);

    @Select("SELECT DATE_FORMAT(create_time,'%Y-%m-%d')dateStr,count(id)count from wnsys_user where to_days(create_time) = to_days(now()) GROUP BY DATE_FORMAT(create_time,'%Y-%m-%d') ORDER BY dateStr")
    @ResultType(UserCountDayVO.class)
    List<UserCountDayVO> countUsersByToday();

    @Select("SELECT DATE_FORMAT(create_time,'%Y-%m-%d')dateStr,count(id)count from wnsys_user where DATEDIFF(create_time,NOW())=-1 GROUP BY DATE_FORMAT(create_time,'%Y-%m-%d') ORDER BY dateStr")
    @ResultType(UserCountDayVO.class)
    List<UserCountDayVO> countUsersByYesterday();

    @Select("SELECT DATE_FORMAT(create_time,'%Y-%m-%d')dateStr,count(id)count from wnsys_user where YEARWEEK(DATE_FORMAT(create_time,'%Y-%m-%d'))=YEARWEEK(NOW()) GROUP BY DATE_FORMAT(create_time,'%Y-%m-%d') ORDER BY dateStr")
    @ResultType(UserCountDayVO.class)
    List<UserCountDayVO> countUsersByWeek();

    @Select("SELECT DATE_FORMAT(create_time,'%Y-%m-%d')dateStr,count(id)count from wnsys_user where DATE_FORMAT(create_time,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') GROUP BY DATE_FORMAT(create_time,'%Y-%m-%d') ORDER BY dateStr")
    @ResultType(UserCountDayVO.class)
    List<UserCountDayVO> countUsersByMonth();


    @Update("update wnsys_user set mobile=#{mobile},password=#{password},name=#{name},vip=#{vip},avatar=#{avatar} where id=#{id}")
    boolean updateUser(User user);

}
